package com.ruoyi.note.service.impl;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.note.domain.TBookContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.note.mapper.TBookKeyMapper;
import com.ruoyi.note.domain.TBookKey;
import com.ruoyi.note.service.ITBookKeyService;

/**
 * 词条Service业务层处理
 *
 * @author cl
 * @date 2021-05-31
 */
@Service
public class TBookKeyServiceImpl implements ITBookKeyService {
    @Autowired
    private TBookKeyMapper tBookKeyMapper;

    /**
     * 查询词条
     *
     * @param id 词条ID
     * @return 词条
     */
    @Override
    public TBookKey selectTBookKeyById(Long id) {
        return tBookKeyMapper.selectTBookKeyById(id);
    }

    /**
     * 查询词条列表
     *
     * @param tBookKey 词条
     * @return 词条
     */
    @Override
    public List<TBookKey> selectTBookKeyList(TBookKey tBookKey) {
        return tBookKeyMapper.selectTBookKeyList(tBookKey);
    }

    @Override
    public List<TBookKey> selectTBookKeyByContentId(Long contentId) {
        return tBookKeyMapper.selectTBookKeyByContentId(contentId);
    }

    /**
     * 新增词条
     *
     * @param tBookKey 词条
     * @return 结果
     */
    @Override
    public int insertTBookKey(TBookKey tBookKey) {
        tBookKey.setCreateTime(DateUtils.getNowDate());
        return tBookKeyMapper.insertTBookKey(tBookKey);
    }

    /**
     * 修改词条
     *
     * @param tBookKey 词条
     * @return 结果
     */
    @Override
    public int updateTBookKey(TBookKey tBookKey) {
        return tBookKeyMapper.updateTBookKey(tBookKey);
    }

    /**
     * 批量删除词条
     *
     * @param ids 需要删除的词条ID
     * @return 结果
     */
    @Override
    public int deleteTBookKeyByIds(Long[] ids) {
        return tBookKeyMapper.deleteTBookKeyByIds(ids);
    }

    /**
     * 删除词条信息
     *
     * @param id 词条ID
     * @return 结果
     */
    @Override
    public int deleteTBookKeyById(Long id) {
        return tBookKeyMapper.deleteTBookKeyById(id);
    }

    @Override
    public int deleteTBookKeyByContentId(Long id) {
        return tBookKeyMapper.deleteTBookKeyByContentId(id);
    }

    @Override
    public int handlerByJson(TBookContent content, String json) {
        deleteTBookKeyByContentId(content.getId());

        JSONArray array = JSONObject.parseArray(json);
        for (Object o : array) {
            JSONObject object = (JSONObject) o;
            TBookKey bookKey = new TBookKey(content);
            bookKey.setTitle(object.getString("title"));
            bookKey.setContent(object.getString("content"));
            if (StringUtils.isNotEmpty(bookKey.getTitle()))
                this.tBookKeyMapper.insertTBookKey(bookKey);
        }
        return 0;
    }

    @Override
    public int countAll(Long userId) {
        return tBookKeyMapper.countAll(userId);
    }

    @Override
    public List<Map> addTrend(Long userId) {
        return tBookKeyMapper.addTrend(userId);
    }
}
